﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using BLL;
using Util;
public partial class Mother : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) 
        {
            CheckUser();
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        string userName = this.userName.Value;
        string pwd = this.pwd.Value;
        if (string.IsNullOrWhiteSpace(userName) || string.IsNullOrWhiteSpace(pwd))
        {
            Util.UtilWeb.Message(this.Page, Util.KoInfo.LoginEmpty, null);
            return;
        }
        tb_user user = new BllUser().GetSingle(c => c.name.Equals(userName) && c.pwd.Equals(pwd));
        if (user != null)
        {

            tb_user u = new tb_user() { id = user.id, name = userName };
            Session.Add(Util.KoInfo.CookieUser, u);
            Response.Redirect("download.aspx");
           
        }

        CheckUser();
    }

    private void CheckUser()
    {
        object obj = Session[Util.KoInfo.CookieUser];
        if (obj != null)
        {
            // var user = new BllUse().GetSingle(int.Parse(id));
            this.login.Visible = false;
            //System.Web.UI.HtmlControls.HtmlControl div = Page.Master.FindControl("focus") as System.Web.UI.HtmlControls.HtmlControl;
            // div.Visible = false;
           

            this.ltlUserInfo.Text = (obj as tb_user).name;
            this.Log.Visible = true;
          
        }
        else
        {
            this.login.Visible = true;
            // System.Web.UI.HtmlControls.HtmlControl div = Page.Master.FindControl("focus") as System.Web.UI.HtmlControls.HtmlControl;
            // div.Visible = true;
            this.Log.Visible = false;
        }
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        try
        {
          
            Logout();
            GoLogin();
        }
        catch
        {


        }
    }
    public bool Logout()
    {
        //HttpCookie cookie = Request.Cookies[Util.KoInfo.CookieUser];
        //cookie.Expires = DateTime.Now.AddDays(-1);
        //Response.Cookies.Add(cookie);

        //  Session.Remove(Util.KoInfo.CookieAdmin);
        Session.Clear();
        return true;
    }
    public void GoLogin()
    {
        Util.UtilWeb.Message(this.Page, null, "window.open('index.aspx','_parent')");
    }
}
